<?
/*
 * emarket - a website to create your own online-shop.  
 * Copyright (C) 2009  Frédéric Delorme
 *
 * This file is part of emarket.
 *
 * emarket is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * emarket is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with emarket.  If not, see <http://www.gnu.org/licenses/>.
 *
 * To contact me: frederic.delorme@gmail.com
 * to see some information on te project : http://code.google.col/p/os-emarket/
 *
 */
class Debug{
	static private $_instance;
	static private $outputfile;
	private $logfilename;
	static private $filteredClass = array();
	static private $levelToLog = array();
	static private $outputdiv;
	/**
	 * default constructor of the class. this is a demo implementation
	 * retriuving all data from data/*.dat files, where filename is name of
	 * the entity to set available in the application.
	 */
	function __construct(){
		//echo "debug:construct";
		if(in_array(__param("debug","activated"),array("on","true","1"))){
			$filename=__param("debug","logfilename");
			$filename = str_replace("%APPNAME%",__param("system","appname"),$filename);
			$filename = str_replace("%DATE%",date( __param("debug","datefileformat") ),$filename);
			$this->logfilename = (!empty($filename)&&$filename!="" ? $filename : "log/webapp.log");
			//echo "logfilename:".$this->logfilename."<br/>";
			if(!file_exists(dirname($_SERVER['SCRIPT_FILENAME'])."/".$this->logfilename)){
				$this->outputfile = fopen(dirname($_SERVER['SCRIPT_FILENAME'])."/".$this->logfilename,"a+");
				if($this->outputfile){
					fprintf($this->outputfile,"Create Log on "+date("Ymd"));
					fclose($this->outputfile);
				}
			}
			if(__param('debug','filteredClass')!=""){
				self::$filteredClass=split(",",__param('debug','filteredClass'));
			}
			if(__param('debug','level')!=""){
				self::$levelToLog=split(",",__param('debug','level'));
			}
		}
		$this->outputdiv="";
	}
	
	/**
	 * output a $message on the log file according to the defined severity $level,
	 * coming from $line into the $file.
	 * @param $level
	 * @param $message
	 * @param $file
	 * @param $line
	 */
	function message($level="INFO",$message,$file="",$line=""){
		try{
			if(in_array(__param("debug","activated"),array("on","true","1"))&&
				!$this->isFiltered($message) &&
				in_array($level,self::$levelToLog)){
				$this->outputfile = fopen(dirname($_SERVER['SCRIPT_FILENAME'])."/".$this->logfilename,"a+");
				if($this->outputfile){
					if(__param_is_activated('debug','file_and_line')){
						fprintf($this->outputfile,date("Y/m/d-h:i:s").";"."$level;$message;".($file!=""?"$file;":";").($line!=""?"$line;":";")."\n");
					}else{
						if($message!="" && $level!=""){
							fprintf($this->outputfile,date("Y/m/d-h:i:s").";".$level.";".$message."\n");
						}
					}
					fclose($this->outputfile);
				}
			}
			if($level=="DEBUG"){
				$this->outputdiv .= "<p>".$message."</p>";
			}
		}catch(Exception $e){
			echo $e.":message=[$message],level=[$level];";
		}
	}
	
	/**
	 * Return true if the class is one of the filtered classes defined into the config.ini file
	 * in the parameter debug/filteredClass (string with values separated by ",").
	 * @param $message to be tested.
	 * @return boolean value. true if $message concern a filtered classes.
	 */
	private function isFiltered($message){
		foreach(self::$filteredClass as $classe){
			if(strpos($message,$classe)!==false){
				return true;
			}
		}
		return false;
	}
	
	/**
	 * return the instance of the Debug component.
	 * @return Debug instance.
	 */
	public function getInstance(){
		if(empty(self::$_instance)){
			self::$_instance = new Debug();
		}
		return self::$_instance;
	}
	public function displayDebug(){
		$flag=__param("system","displaydebug");
		if($flag=="on"||$flag=="ON"||$flag="1"){
			return "<div id=\"debug\">".$this->outputdiv."</div>";
		}else{
			return "";
		}
	}
}
?>